package com.tjshop.android.view;

import com.tjshop.android.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.View;

public class IndicatorView extends View {

	private Context mContext;

	private int pointNum = 4;

	private final int DISTANCE = 8;

	private int selectedIndex = 2;

	Bitmap normal, selected;

	public IndicatorView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		init();
	}

	public IndicatorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		init();
	}

	public IndicatorView(Context context) {
		super(context);
		this.mContext = context;
		init();
	}

	private void init() {
		normal = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.product_point_normal);
		selected = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.product_point_selected);
	}

	public int getPointNum() {
		return pointNum;
	}

	public void setPointNum(int pointNum) {
		this.pointNum = pointNum;
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = selectedIndex;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG));
		for (int i = 0; i < pointNum; i++) {
			if (i == selectedIndex) {
				canvas.drawBitmap(selected, getWidth() / 2 - DISTANCE
						* ((pointNum - 1) - (2 * i)) - selected.getWidth()
						* (pointNum - 2 * i) / 2,
						getHeight() - selected.getHeight() - 3, new Paint());
			} else {
				canvas.drawBitmap(normal, getWidth() / 2 - DISTANCE
						* ((pointNum - 1) - (2 * i)) - selected.getWidth()
						* (pointNum - 2 * i) / 2,
						getHeight() - normal.getHeight() - 3, new Paint());
			}

		}
	}

}
